package com.yanze.schoolinformation.viewModule.gank;

import com.yanze.schoolinformation.app.C;
import com.yanze.schoolinformation.model.bean.WanAndroidBannerBean;
import com.yanze.schoolinformation.model.bean.WanAndroidListBean;
import com.yanze.schoolinformation.model.http.DeforObserver;
import com.yanze.schoolinformation.model.http.reqo.DataReqo;
import com.yanze.schoolinformation.utils.ToastUtils;
import com.yanze.schoolinformation.view.information.gank.WanAndroidFragment;

import java.util.ArrayList;
import java.util.List;

import io.reactivex.disposables.Disposable;

/**
 * Created by MrZheng on 2018/4/28.
 */

public class WanAndroidViewModel {

    private WanAndroidFragment fragment;
    private WanAndroidNavigator mNavigator;
    private int page = 0;
    private ArrayList<String> mBannerImages;
    private ArrayList<String> mBannerTitles;

    public WanAndroidViewModel(WanAndroidFragment fragment) {
        this.fragment = fragment;
    }

    public void setNavigator(WanAndroidNavigator mNavigator) {
        this.mNavigator = mNavigator;
    }

    public int getPage() {
        return page;
    }

    public void setPage(int page) {
        this.page = page;
    }

    //加载Banner数据
    public void loadBannerData() {
        DataReqo.getInstant().getWanAndroidBanner(C.url.API_WAN_ANDROID)
                .subscribe(new DeforObserver<WanAndroidBannerBean>() {
                    @Override
                    public void _onSubscribe(Disposable disposable) {
                        fragment.addDisposable(disposable);
                    }

                    @Override
                    public void _onNext(WanAndroidBannerBean entity) {
                        mNavigator.showLoadSuccessView();
                        if (mBannerImages == null) {
                            mBannerImages = new ArrayList<String>();
                        } else {
                            mBannerImages.clear();
                        }
                        if (mBannerTitles == null) {
                            mBannerTitles = new ArrayList<String>();
                        } else {
                            mBannerTitles.clear();
                        }
                        List<WanAndroidBannerBean.DataBean> result = null;
                        if (entity != null && entity.getData() != null) {
                            result = entity.getData();
                            if (result != null && result.size() > 0) {
                                for (int i = 0; i < result.size(); i++) {
                                    //获取所有图片
                                    mBannerImages.add(result.get(i).getImagePath());
                                    mBannerTitles.add(result.get(i).getTitle());
                                }
                                mNavigator.showBannerView(mBannerImages, mBannerTitles, result);
                            }
                        }
                        if (result == null) {
                            mNavigator.loadBannerFailure();
                        }
                    }

                    @Override
                    public void _onError(String msg) {
                        mNavigator.loadBannerFailure();
                    }
                });
    }

    //加载列表数据
    public void loadListData() {
        DataReqo.getInstant().getWanaAndroidList(C.url.API_WAN_ANDROID, page)
                .subscribe(new DeforObserver<WanAndroidListBean>() {
                    @Override
                    public void _onSubscribe(Disposable disposable) {
                        fragment.addDisposable(disposable);
                    }

                    @Override
                    public void _onNext(WanAndroidListBean entity) {
                        mNavigator.showLoadSuccessView();
                        if (page == 0) {
                            if (entity != null && entity.getData() != null && entity.getData().getDatas() != null && entity.getData().getDatas().size() > 0) {
                                mNavigator.showAdapterView(entity);
                            } else {
                                mNavigator.loadHomeListFailure();
                            }
                        } else {
                            if (entity != null && entity.getData() != null && entity.getData().getDatas() != null && entity.getData().getDatas().size() > 0) {
                                mNavigator.refreshAdapter(entity);
                            } else {
                                mNavigator.showListNoMoreLoading();
                            }
                        }
                    }

                    @Override
                    public void _onError(String msg) {
                        ToastUtils.show(msg);
                        mNavigator.loadHomeListFailure();
                        if (page > 0) {
                            page--;
                        }
                    }
                });
    }

    public void onDestory() {
        mNavigator = null;
        fragment = null;
    }
}
